import { createPinia } from "pinia";

export const piniaStore = createPinia();
import { defineStore } from "pinia";
import { watch } from "vue";
import { piniaStateType_format, Shop, User_format, UserStoreType_format } from "../type/index";
import { getUserInfo } from "@/api";
//一些组件展示的状态
export const usePiniaState = defineStore({
  id: "textPinia",
  state: () => {
    return {
      isLoading: false,
      tabBarIsShow: true,
    };
  },
});

//常用的信息(搜索历史,位置等)
const constId = "pinia";
export const constPinia = defineStore({
  id: "const",
  state: () => {
    if (localStorage.getItem(constId)) {
      return JSON.parse(localStorage.getItem(constId)) as piniaStateType_format;
    } else {
      return {
        latitude: null,
        longitude: null,
        foodSearchList: [],
        addressHistory: [],
        geohash: "",
        nearByAddress: null,
      } as piniaStateType_format;
    }
  },
  actions: {
    setSearchHistory(value: { [props: string]: any }) {
      const newHistory = this.addressHistory.filter(item => {
        return item.name !== value.name;
      });
      newHistory.unshift(value);
      this.addressHistory = newHistory;
      this.latitude = value.latitude;
      this.longitude = value.longitude;
      this.geohash = value.geohash;
    },

    setSearchFoodHistory(value: string | number) {
      const newHistory = this.foodSearchList.filter(item => {
        return item !== value;
      });
      newHistory.unshift(value);
      this.foodSearchList = newHistory;
    },

    clearSearchFoodHistory(mode: "all" | "single", value?: string | number) {
      if (mode == "all") {
        this.foodSearchList = [];
      } else if (mode == "single") {
        const arr = this.foodSearchList.filter(item => {
          return item != value;
        });
        this.foodSearchList = arr;
      }
    },
  },
});

const ConstStore = constPinia(piniaStore);
watch(
  ConstStore.$state,
  state => {
    localStorage.setItem(constId, JSON.stringify(state));
  },
  { deep: true }
);

//购物车
const shopId = "shop";
export const shop = defineStore({
  id: "shop",
  state: () => {
    if (localStorage.getItem(shopId)) {
      return { cartList: JSON.parse(localStorage.getItem(shopId)) } as Shop;
    } else {
      return {
        cartList: {},
      } as Shop;
    }
  },
  getters: {},
  actions: {
    addCart(
      shopId: number | string,
      category_id: number,
      item_id: number,
      food_id: number,
      name: string,
      price: number,
      specs: string,
      packing_fee: number,
      sku_id: number,
      stock: number
    ) {
      let cartList = this.$state.cartList;
      let shop = (cartList[shopId] = cartList[shopId] || {});
      let cate = (shop[category_id] = shop[category_id] || {});
      let item = (cate[item_id] = cate[item_id] || {});
      if (item[food_id]) {
        item[food_id].num++;
      } else {
        item[food_id] = {
          num: 1,
          id: food_id,
          name,
          price,
          specs,
          packing_fee,
          sku_id,
          stock,
        };
      }
      this.$state.cartList = { ...cartList };
    },
    reduceCart(shopId: number | string, category_id: number, item_id: number, food_id: number) {
      let cartList = this.$state.cartList;
      let shop = (cartList[shopId] = cartList[shopId] || {});
      let cate = (shop[category_id] = shop[category_id] || {});
      let item = (cate[item_id] = cate[item_id] || {});
      if (item[food_id].num > 0) {
        item[food_id].num--;
        if (item[food_id].num == 0) {
          delete item[food_id];
        }
      }
      this.$state.cartList = { ...cartList };
    },
    removeCart(shopId: number | string) {
      this.$state.cartList[shopId] = null;
    },
  },
});
const ShopStore = shop(piniaStore);
watch(
  ()=>ShopStore.$state.cartList,
  state => {
    localStorage.setItem(shopId, JSON.stringify(state));
  },
  { deep: true }
);

//用户信息
const userId = "user";
const addressId = "selectAddress";
export const user = defineStore({
  id: "user",
  state: () => {
    const store = {
      userinfo: null,
      user_id: null,
      selectAddress: null,
      remark:''
    };
    if (localStorage.getItem(userId)) store.user_id = JSON.parse(localStorage.getItem(userId));
    if (localStorage.getItem(addressId))
      store.selectAddress = JSON.parse(localStorage.getItem(addressId));
    return store as UserStoreType_format;
  },
  actions: {
    logout() {
      this.$state.user_id = null;
      this.$state.userinfo = null;
      localStorage.removeItem(userId);
    },
  },
});
const UserStore = user(piniaStore);
watch(
  () => UserStore.$state.user_id,
  value => {
    localStorage.setItem(userId, JSON.stringify(value));
  }
);
watch(
  () => UserStore.$state.selectAddress,
  value => {
    localStorage.setItem(addressId, JSON.stringify(value));
  }
);
